/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdAsignaDatos.java
 *
 * Created on 23-ago-2010, 8:52:38
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.DescripcionTiposTO;
import seguridad.administracion.ejb30.TO.TipoDatoTO;

/**
 *
 * @author Luis_Angel
 */
public class jdAsignaDatos extends javax.swing.JDialog {

    private String id_usuario;
    private JTableBinding jTableBinding;
    private List<DescripcionTiposTO> tipos_de_usuario_temp;
    private List<DescripcionTiposTO> tipos_de_usuario;
    private BindingGroup bindingGroup1;

    /** Creates new form jdAsignaDatos */
    public jdAsignaDatos(java.awt.Frame parent, boolean modal,String id_usuario) {
        super(parent, modal);
        initComponents();
        llenarComboTiposDato();
         this.id_usuario = id_usuario;
        try {
            this.tipos_de_usuario_temp = Listas.getDescripcionTiposDeUsuario(id_usuario);
        } catch (Exception ex) {
            Logger.getLogger(jdAsignaDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
         //actualizaTablaDescripcionDatos(Listas.getDescripcionTiposDato());
         actualizaTablaDescripcionDatosDeUsuario(this.tipos_de_usuario_temp);
    }

    private void llenarComboTiposDato() {
            List listaZonas = null;
            List listaZonas_cbo = this.cboBeanBindingTipoDato1.getLista();
            listaZonas_cbo.clear();
            try
            {
              listaZonas = Listas.getTiposDato();
              this.cboBeanBindingTipoDato1.setLista(listaZonas);
              this.bindingGroup.bind();
              this.jcboTiposDato.updateUI();
              this.jcboTiposDato.setSelectedIndex(0);
            } catch (Exception ex) {
              Utiles_MensajesDialogo.mensajeAlerta("Error al obtener tipos de dato\n" + ex.getMessage());
              ex.printStackTrace();

            }
     }



     private void actualizaTablaDescripcionDatos(List<DescripcionTiposTO> descripciones) {
            this.bindingGroup1 = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, descripciones, this.jtDescripciones);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${nom_tipo_dato}"));

            columnBinding0.setColumnName("Tipo");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${descripcion_dato}"));

            columnBinding1.setColumnName("Descripcion");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);

            this.bindingGroup1.addBinding(this.jTableBinding);
            this.bindingGroup1.bind();

        }

     private void actualizaTablaDescripcionDatosDeUsuario(List<DescripcionTiposTO> descripciones) {
            this.bindingGroup = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, descripciones, this.jtTipos_de_usuario);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${nom_tipo_dato}"));

            columnBinding0.setColumnName("Tipo");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${descripcion_dato}"));

            columnBinding1.setColumnName("Descripcion");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


        }

     private void limpiar(){
         this.tipos_de_usuario.clear();
         actualizaTablaDescripcionDatos(this.tipos_de_usuario);
         this.tipos_de_usuario_temp.clear();
         actualizaTablaDescripcionDatosDeUsuario(this.tipos_de_usuario_temp);

     }
     
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBeanBindingTipoDato1 = new administracion.BeanBinding.CboBeanBindingTipoDato();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtTipos_de_usuario = new javax.swing.JTable();
        jbCancelar1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDescripciones = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jcboTiposDato = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jtTipos_de_usuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo", "Descripcion"
            }
        ));
        jtTipos_de_usuario.setName("jtTipos_de_usuario"); // NOI18N
        jScrollPane2.setViewportView(jtTipos_de_usuario);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdAsignaDatos.class);
        jbCancelar1.setFont(resourceMap.getFont("jbCancelar1.font")); // NOI18N
        jbCancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar1.setMnemonic('C');
        jbCancelar1.setText(resourceMap.getString("jbCancelar1.text")); // NOI18N
        jbCancelar1.setToolTipText(resourceMap.getString("jbCancelar1.toolTipText")); // NOI18N
        jbCancelar1.setName("jbCancelar1"); // NOI18N
        jbCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelar1ActionPerformed(evt);
            }
        });
        jbCancelar1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelar1KeyPressed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtDescripciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo", "Descripcion"
            }
        ));
        jtDescripciones.setName("jtDescripciones"); // NOI18N
        jScrollPane1.setViewportView(jtDescripciones);

        jButton3.setFont(resourceMap.getFont("jButton3.font")); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/save_all.png"))); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jcboTiposDato.setName("jcboTiposDato"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingTipoDato1, eLProperty, jcboTiposDato);
        bindingGroup.addBinding(jComboBoxBinding);

        jcboTiposDato.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcboTiposDatoItemStateChanged(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/list_add.png"))); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/list_remove.png"))); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jcboTiposDato, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 205, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(111, 111, 111))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(241, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(jbCancelar1)
                .addGap(228, 228, 228))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jcboTiposDato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCancelar1)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelar1ActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelar1ActionPerformed

    private void jbCancelar1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelar1KeyPressed
        
}//GEN-LAST:event_jbCancelar1KeyPressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            if(GestionSeguridadDelegate.getInstance().agregaTiposUsuario(tipos_de_usuario_temp, id_usuario)){
                Utiles_MensajesDialogo.mensajeExito();
                limpiar();
                this.dispose();
                Listas.refreshDatosUsuario();
            }
        } catch (Exception ex) {
            Utiles_MensajesDialogo.mensajeError("No se pudo guardar los tipos de dato ");
        }
}//GEN-LAST:event_jButton3ActionPerformed

    private void jcboTiposDatoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcboTiposDatoItemStateChanged
        this.tipos_de_usuario = Listas.getDescripcionTiposDatoPorId(((TipoDatoTO)this.jcboTiposDato.getSelectedItem()).getId_tipo());
        actualizaTablaDescripcionDatos( this.tipos_de_usuario);
}//GEN-LAST:event_jcboTiposDatoItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        DescripcionTiposTO tipo;
        int filaElegida = this.jtDescripciones.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un Tipo!");
            this.jtDescripciones.requestFocus();
            return;
        }

        try {
            tipo = this.tipos_de_usuario.get(filaElegida);
            int sw = 0;
            for(DescripcionTiposTO dto : tipos_de_usuario_temp){
                if(dto.equals(tipo)){
                    Utiles_MensajesDialogo.mensajeError("Ya se encuentra asignado");
                    sw=1;
                    break;
                }

            }
            if(sw==0){
                this.tipos_de_usuario_temp.add(tipo);
                 actualizaTablaDescripcionDatosDeUsuario(this.tipos_de_usuario_temp);
            }

        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo cargar  el tipo\n" + e.getMessage());
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        int filaElegida = this.jtTipos_de_usuario.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un Tipo!");
            this.jtTipos_de_usuario.requestFocus();
            return;
        }

        try {
            this.tipos_de_usuario_temp.remove(filaElegida);
            actualizaTablaDescripcionDatosDeUsuario(this.tipos_de_usuario_temp);
        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo cargar  el tipo\n" + e.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private administracion.BeanBinding.CboBeanBindingTipoDato cboBeanBindingTipoDato1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbCancelar1;
    private javax.swing.JComboBox jcboTiposDato;
    private javax.swing.JTable jtDescripciones;
    private javax.swing.JTable jtTipos_de_usuario;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
